﻿package com.daowen.action;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SQLQuery;
import org.hibernate.Query;
import com.daowen.dal.*;
import com.daowen.bll.*;
import com.daowen.entity.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.text.MessageFormat;
import com.daowen.util.PagerMetal;

/**************************
 * 
 * @author daowen
 * 
 *         卡片管理控制
 * 
 */
public class BankcardAction extends PageActionBase {
	@Override
	public void onLoad() {
		String actiontype = request.getParameter("actiontype");
		System.out.println("actiontype=" + actiontype);
		if (actiontype == null)
			return;
	}

	/********************************************************
	 ****************** 信息注销监听支持*****************************
	 *********************************************************/
	public void delete() {
		String id = request.getParameter("id");
		DALBase.delete("bankcard", " where id=" + id);
		binding();
	}

	/*************************************************************
	 **************** 保存动作监听支持******************************
	 **************************************************************/
	public void save() {
		String forwardurl = request.getParameter("forwardurl");
		// 验证错误url
		String errorurl = request.getParameter("errorurl");
		String cardno = request.getParameter("cardno");
		String bankname = request.getParameter("bankname");
		String khid = request.getParameter("khid");
		String khrname = request.getParameter("khrname");
		String khridcardno = request.getParameter("khridcardno");
		SimpleDateFormat sdfbankcard = new SimpleDateFormat("yyyy-MM-dd");
		Bankcard bankcard = new Bankcard();
		bankcard.setCardno(cardno == null ? "" : cardno);
		bankcard.setBankname(bankname == null ? "" : bankname);
		bankcard.setKhid(khid == null ? 0 : new Integer(khid));
		bankcard.setKhrname(khrname == null ? "" : khrname);
		bankcard.setKhridcardno(khridcardno == null ? "" : khridcardno);
		bankcard.setAmount(0.0);
		// 产生验证
		Boolean validateresult = saveValidate("where cardno='" + cardno + "'");
		if (validateresult) {
			try {
				request.setAttribute("errormsg",
						"<label class='error'>已经存在的卡号</label>");
				request.setAttribute("bankcard", bankcard);
				request.setAttribute("actiontype", "save");
				request.getRequestDispatcher(errorurl).forward(request,
						response);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		DALBase.save(bankcard);
		
		Huiyuan kehu=(Huiyuan)DALBase.load("huiyuan", "where id="+khid);
		if(kehu!=null){
			
			DALBase.update(kehu);
			
			
		}
		
		if (forwardurl == null) {
			forwardurl = "/admin/bankcardmanager.do?actiontype=get";
		}
		try {
			response.sendRedirect(SystemParam.getSiteRoot() + forwardurl);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 新增验证
	private boolean saveValidate(String filter) {
		return DALBase.isExist("bankcard", filter);
	}

	/******************************************************
	 *********************** 更新内部支持*********************
	 *******************************************************/
	public void update() {
		String forwardurl = request.getParameter("forwardurl");
		String id = request.getParameter("id");
		if (id == null)
			return;
		Bankcard bankcard = (Bankcard) DALBase.load(Bankcard.class,
				new Integer(id));
		if (bankcard == null)
			return;
		String cardno = request.getParameter("cardno");
		String bankname = request.getParameter("bankname");
		String khid = request.getParameter("khid");
		String khrname = request.getParameter("khrname");
		String khridcardno = request.getParameter("khridcardno");
		SimpleDateFormat sdfbankcard = new SimpleDateFormat("yyyy-MM-dd");
		bankcard.setCardno(cardno);
		bankcard.setBankname(bankname);
		bankcard.setKhid(khid == null ? 0 : new Integer(khid));
		bankcard.setKhrname(khrname);
		bankcard.setKhridcardno(khridcardno);
		DALBase.update(bankcard);
		if (forwardurl == null) {
			forwardurl = "/admin/bankcardmanager.do?actiontype=get";
		}
		try {
			response.sendRedirect(SystemParam.getSiteRoot() + forwardurl);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/******************************************************
	 *********************** 加载内部支持*********************
	 *******************************************************/
	public void load() {
		//
		String id = request.getParameter("id");
		String actiontype = "save";
		dispatchParams(request, response);
		if (id != null) {
			Bankcard bankcard = (Bankcard) DALBase.load("bankcard", "where id="
					+ id);
			if (bankcard != null) {
				request.setAttribute("bankcard", bankcard);
			}
			actiontype = "update";
			request.setAttribute("id", id);
		}
		request.setAttribute("actiontype", actiontype);
		String forwardurl = request.getParameter("forwardurl");
		System.out.println("forwardurl=" + forwardurl);
		if (forwardurl == null) {
			forwardurl = "/admin/bankcardadd.jsp";
		}
		try {
			request.getRequestDispatcher(forwardurl).forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/******************************************************
	 *********************** 数据绑定内部支持*********************
	 *******************************************************/
	public void binding() {
		String filter = "where 1=1 ";
		String khid=request.getParameter("khid");
		String cardno = request.getParameter("cardno");
		if (cardno != null)
			filter += "  and cardno like '%" + cardno + "%'  ";
		//
		if(khid!=null)
			filter+="  and khid="+khid;
		int pageindex = 1;
		int pagesize = 10;
		// 获取当前分页
		String currentpageindex = request.getParameter("currentpageindex");
		// 当前页面尺寸
		String currentpagesize = request.getParameter("pagesize");
		// 设置当前页
		if (currentpageindex != null)
			pageindex = new Integer(currentpageindex);
		// 设置当前页尺寸
		if (currentpagesize != null)
			pagesize = new Integer(currentpagesize);
		List<Bankcard> listbankcard = DALBase.getPageEnity("bankcard", filter,
				pageindex, pagesize);
		int recordscount = DALBase.getRecordCount("bankcard",
				filter == null ? "" : filter);
		request.setAttribute("listbankcard", listbankcard);
		PagerMetal pm = new PagerMetal(recordscount);
		// 设置尺寸
		pm.setPagesize(pagesize);
		// 设置当前显示页
		pm.setCurpageindex(pageindex);
		// 设置分页信息
		request.setAttribute("pagermetal", pm);
		// 分发请求参数
		dispatchParams(request, response);
		String forwardurl = request.getParameter("forwardurl");
		System.out.println("forwardurl=" + forwardurl);
		if (forwardurl == null) {
			forwardurl = "/admin/bankcardmanager.jsp";
		}
		try {
			request.getRequestDispatcher(forwardurl).forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
